package com.example.mattrigle;

import android.text.InputFilter;
import android.text.Spanned;
import android.util.Log;
import android.widget.EditText;

public class InputFilterMinMax implements InputFilter {

    private float min, max;
    private EditText edt;

    public InputFilterMinMax(float min, float max,EditText EDT) {
        this.min = min;
        this.max = max;
        edt=EDT;
    }

    public InputFilterMinMax(String min, String max,EditText EDT) {
        this.min = Float.parseFloat(min);
        this.max = Float.parseFloat(max);
        edt=EDT;
    }


    private boolean isInRange(float a, float b, float c) {
        return b > a ? c >= a && c <= b : c >= b && c <= a;
    }

	@Override
    public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {   

		try {
			//Log.e("edt", "edt:"+edt.length());
            float input =Float.parseFloat(dest.toString() + source.toString());
            if (isInRange(min, max, input)&&edt.length()<5)
            {

             //  Log.e("edt"," edt.length()<5 return null");
                	return null;
               
            }
           
           
            
            
        } catch (NumberFormatException nfe) { }   
        
        
        if(!MainActivity.isBtnAddActive)
        return "";
        else
        	return null;
	}
}
